package com.lanji.sensitive.entity.result;

import com.lanji.sensitive.util.LocalDateUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @author Twilight
 * @desc
 * @createTime 2020-05-17-10:07
 */
@ApiModel(value = "结果数据")
public class ResultData<T> {
    @ApiModelProperty(value = "数据")
    private T data;
    @ApiModelProperty(value = "返回码")
    private Integer code;
    @ApiModelProperty(value = "提示")
    private String msg;
    @ApiModelProperty(value = "时间戳")
    private Long time;
    @ApiModelProperty(value = "格式化时间")
    private String now;
    @ApiModelProperty(value = "分页数据")
    private PageDto pageDto = new PageDto();


    public ResultData(ResultEnum resultEnum) {
        LocalDateTime now = LocalDateTime.now();
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
        this.time = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        this.now = LocalDateUtil.formatter(now);
    }

    public ResultData(ResultEnum resultEnum, T data) {
        LocalDateTime now = LocalDateTime.now();
        this.data = data;
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
        this.time = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        this.now = LocalDateUtil.formatter(now);
    }

    public ResultData(ResultEnum resultEnum, String msg) {
        LocalDateTime now = LocalDateTime.now();
        this.code = resultEnum.getCode();
        this.msg = msg;
        this.time = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        this.now = LocalDateUtil.formatter(now);
    }

    public ResultData(Integer code, String msg, T data) {
        LocalDateTime now = LocalDateTime.now();
        this.data = data;
        this.code = code;
        this.msg = msg;
        this.time = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        this.now = LocalDateUtil.formatter(now);
    }

    public ResultData() {
    }

    public static ResultData success() {
        return new ResultData(ResultEnum.SUCCESS);
    }

    public static ResultData success(String msg) {
        return new ResultData(ResultEnum.SUCCESS, msg);
    }

    public static <T> ResultData success(T data) {
        return new ResultData<T>(ResultEnum.SUCCESS, data);
    }

    public static ResultData fail() {
        return new ResultData(ResultEnum.FAILED);
    }

    public static ResultData fail(String msg) {
        return new ResultData(ResultEnum.FAILED, msg);
    }

    public static <T> ResultData fail(T data) {
        return new ResultData(ResultEnum.FAILED, data);
    }

    public static <T> ResultData custom(Integer code, String msg, T data) {
        return new ResultData(code, msg, data);
    }

    public ResultData page(PageDto pageDto) {
        this.pageDto = pageDto;
        return this;
    }

    public ResultData defaultPage() {
        this.pageDto = new PageDto(1, 10);
        return this;
    }

    public String getNow() {
        return now;
    }

    public void setNow(String now) {
        this.now = now;
    }

    public Long getTime() {
        return time;
    }

    public void setTime(Long time) {
        this.time = time;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public PageDto getPageDto() {
        return pageDto;
    }

    public void setPageDto(PageDto pageDto) {
        this.pageDto = pageDto;
    }
}
